import { writeFileSync } from 'graceful-fs';
import { exec } from 'shelljs';
export class FeatureList {
  constructor (projectType: string, featureList: Array<string>) {
    console.log(featureList);
    // 1. 创建项目目录
    this.createProjectFile(projectType, featureList);
  }

  private createProjectFile (projectType: string, featureList: string[]) {
    // 判断项目类型
    // 根据项目类型实现对应功能
    switch (projectType) {
      case 'vue':
        this.createVueFeature(featureList);
        break;
      case 'react':
        // this.createReactFeature(featureList);
        break;
      case 'express':
        // this.createExpressFeature(featureList);
        break;
      default:
    }
  }

  private createVueFeature (featureList: string[]) {
    if (featureList.includes('eslint')) {
      this.createEslint();
    }
    if (featureList.includes('commitizen')) {
      this.createCommitizen();
    }
    if (featureList.includes('husky')) {
      this.createHusky();
    }
    if (featureList.includes('typescript')) {
      this.createTypescript();
    }
  }

  private createTypescript () {
    // throw new Error('Method not implemented.');
  }

  private createHusky () {
    // throw new Error('Method not implemented.');
  }

  private createCommitizen () {
    // throw new Error('Method not implemented.');
  }

  private createEslint () {
    const eslintConfig = `module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: [
    'standard',
    'eslint:recommended',
    'plugin:vue/essential'
  ],
  parserOptions: {
    ecmaVersion: 12,
    sourceType: 'module'
  },
  plugins: [
    'vue'
  ],
  rules: {
    'vue/multi-word-component-names': 'off',
    camelcase: 'off',
    'prefer-promise-reject-errors': 'off',
    'prefer-regex-literals': 'off'
  }
}`;
    writeFileSync('.eslintrc.js', eslintConfig, { encoding: 'utf8' });
    exec('npm cache clear --force');
    // exec('npm install');
    exec('npm install eslint eslint-config-standard eslint-plugin-vue -D --force');
  }
}
